package com.fcl.oschina.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;

import com.fcl.mylibrary.recyclerviewhelper.BaseAdapter;
import com.fcl.mylibrary.util.FLog;
import com.fcl.oschina.R;
import com.fcl.oschina.adapter.CommentAdapter;
import com.fcl.oschina.adapter.CommentAdapter;
import com.fcl.oschina.entity.CommentEntity;
import com.fcl.oschina.model.CommentModel;

import java.util.List;

/**
 * Created by Administrator on 2017/3/20.
 */

public class CommentFragment extends BaseRecyclerFragment<CommentEntity> implements CommentView {

    public static final int COMMENT_TYPE_NEWS = 0;
    public static final int COMMENT_TYPE_TWEET = 1;

    private String TAG = "CommentFragment";
    private CommentModel mCommentModel;
    private CommentAdapter mCommentAdapter;
    private int mId, mType;

    @Override
    protected void initData() {
        super.initData();
        Bundle bundle = getArguments();
        mId = bundle.getInt("id");
        mType = bundle.getInt("type");
        mCommentModel = new CommentModel(this);
        mCommentModel.setNextPage("");
        mCommentModel.getCommentList(mId+"", mType, "refer,reply");

        mCommentAdapter = (CommentAdapter) mAdapter;
        mCommentAdapter.setItemClickListener(new BaseAdapter.MyClickListener() {
            @Override
            public void onClick(int pos) {
//                ToastUtil.showDefineToast("点击位置:"+pos);
//                CommentEntity commentEntity = mDatas.get(pos);
//                jumpActivity(commentEntity.getType(), commentEntity.getId());
            }
        });
    }

    @Override
    protected RecyclerView.Adapter getAdapter() {
        return new CommentAdapter(getContext(), R.layout.item_comment,  mDatas);
    }

    @Override
    protected void onRefresh() {
        super.onRefresh();
        mSceneLayout.setLoading();
        mCommentModel.setNextPage("");
        mCommentModel.getCommentList(mId+"", mType, "refer,reply");
    }

    @Override
    public void setComment(final List<CommentEntity> data, final boolean add) {
        FLog.error(TAG, " thread" + Thread.currentThread().getId());
        if (add) {
            mDatas.addAll(data);
        } else {
            mDatas = data;
        }
        mCommentAdapter.setDatas(mDatas);
        mStickyLayout.finishRefresh();
        mSceneLayout.setNormal();
        if (data.size() < 20) {
            mCommentAdapter.noMoreData();
        }
    }

    @Override
    public void noMoreData() {
        mCommentAdapter.noMoreData();
    }

    @Override
    protected void loadMore(int curPage) {
        super.loadMore(curPage);
        mCommentModel.getCommentList(mId+"", mType, "refer,reply");
    }
}
